package com.subject.shuati.application.controller;


import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.subject.shuati.application.covert.SubjectLabelDtoConverter;
import com.subject.shuati.application.dto.SubjectLabelDto;
import com.subject.shuati.common.entity.Result;
import com.subject.shuati.domain.entity.SubjectLabelBo;
import com.subject.shuati.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题目标签控制层
 */
@RestController
@RequestMapping("/subject/label")
@Slf4j
public class SubjectLabelController {

    @Resource
    private SubjectLabelDomainService labelDomainService;

    /**
     * 新增标签
     * @param subjectLabelDto
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDto subjectLabelDto){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectLabelController.add.dto:{}", JSON.toJSONString(subjectLabelDto));
            }
            Preconditions.checkNotNull(subjectLabelDto.getLabelName(),"标签名不能为空");
            SubjectLabelBo subjectLabelBo = SubjectLabelDtoConverter.
                    INSTANCE.labelDtoConverterLabelBo(subjectLabelDto);
            Boolean result =  labelDomainService.addLabel(subjectLabelBo);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectLabelController.add.error:{}"+e.getMessage(),e);
            return Result.fail("新增标签失败");
        }
    }

    /**
     * 更新题目标签
     * @param subjectLabelDto
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDto subjectLabelDto){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectLabelController.update.dto:{}", JSON.toJSONString(subjectLabelDto));
            }
            Preconditions.checkNotNull(subjectLabelDto.getId(),"标签ID不能为空");
            SubjectLabelBo subjectLabelBo = SubjectLabelDtoConverter.
                    INSTANCE.labelDtoConverterLabelBo(subjectLabelDto);
            Boolean result = labelDomainService.update(subjectLabelBo);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectLabelController.update.error:{}"+e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 标签逻辑删除
     * @param subjectLabelDto
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDto subjectLabelDto){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectLabelController.delete.dto:{}", JSON.toJSONString(subjectLabelDto));
            }
            Preconditions.checkNotNull(subjectLabelDto.getId(),"标签ID不能为空");
            SubjectLabelBo subjectLabelBo = SubjectLabelDtoConverter.
                    INSTANCE.labelDtoConverterLabelBo(subjectLabelDto);
            Boolean result = labelDomainService.delete(subjectLabelBo);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectCategoryController.delete.error:{}"+e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询某个分类下的所有标签
     */
    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDto>> queryLabelByCategoryId(@RequestBody SubjectLabelDto subjectLabelDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.queryCategoryByPrimary.dto:{}"
                        , JSON.toJSONString(subjectLabelDto));
            }
            Preconditions.checkNotNull(subjectLabelDto.getCategoryId(), "分类id不能为空");
            SubjectLabelBo subjectLabelBo = SubjectLabelDtoConverter.
                    INSTANCE.labelDtoConverterLabelBo(subjectLabelDto);
            List<SubjectLabelBo> subjectLabelBoList = labelDomainService.queryLabelByCategoryId(subjectLabelBo);
            List<SubjectLabelDto> subjectLabelDtoList = SubjectLabelDtoConverter.
                    INSTANCE.converterToLabelDtoList(subjectLabelBoList);
            return Result.ok(subjectLabelDtoList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryCategoryByPrimary.error:{}", e.getMessage(), e);
            return Result.fail("查询标签失败");
        }
    }

}
